package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.component.CacheManger;
import com.moli.iov.admin.model.vo.req.RoleVo;
import com.moli.iov.admin.service.IRoleFacadeService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.enums.UserStatusEnum;
import com.moli.iov.sys.model.dto.req.RoleDto;
import com.moli.iov.sys.model.dto.req.RolePermissionDto;
import com.moli.iov.sys.model.dto.req.UserDto;
import com.moli.iov.sys.model.po.Role;
import com.moli.iov.sys.model.po.User;
import com.moli.iov.sys.remote.IRoleRemoteService;
import com.moli.iov.sys.remote.IUserRemoteService;
import com.moli.iov.util.CommonUtils;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 角色管理
 * </p>
 *
 * @author zql
 * @since 2019/9/6 16:35
 */
@Slf4j
@Service
public class RoleFacadeServiceImpl implements IRoleFacadeService {

  @Reference(group = "${service.group}", check = false)
  private IRoleRemoteService roleRemoteService;

  @Reference(group = "${service.group}", check = false)
  private IUserRemoteService userRemoteService;

  @Autowired
  private CacheManger cacheManger;

  /**
   *
   * 查询角色列表
   *
   * @param roleVo
   * @return
   */
  @Override
  public RestResponse<List<Role>> listRole(RoleVo roleVo) {
    return roleRemoteService.listRole(convert(roleVo));
  }

  /**
   *
   * 分页查询角色列表
   *
   * @param roleVo
   * @return
   */
  @Override
  public RestResponse<PageExtend<Role>> listRoleOfPage(RoleVo roleVo) {
    return roleRemoteService.listRoleOfPage(convert(roleVo));
  }

  /**
   * 增加角色
   * @param roleVo
   * @return
   */
  @Override
  public RestResponse saveRole(RoleVo roleVo) {

    RoleDto roleDto = convert(roleVo);

    return roleRemoteService.saveRole(roleDto);
  }

  /**
   * 修改角色
   * @param roleVo
   * @return
   */
  @Override
  public RestResponse updateRoleByRoleId(RoleVo roleVo) {
    return roleRemoteService.updateRoleByRoleId(convert(roleVo));
  }

  /**
   * 设置权限
   * @param roleVo
   * @return
   */
  @Override
  public RestResponse setPermission(RoleVo roleVo) {
    RestResponse restResponse = roleRemoteService.setPermission(convert(roleVo));
    if(restResponse.flag){
      //清空权限缓存
      removeApiPermissions(roleVo.getRoleId());
    }
    return restResponse;
  }

  /**
   * 权限详情
   * @param roleVo
   * @return
   */
  @Override
  public RestResponse<RolePermissionDto> getRolePermissions(RoleVo roleVo) {
    return roleRemoteService.getRolePermissions(convert(roleVo));
  }

  /**
   * 修改权限清空登录api权限缓存
   * @param roleId
   */
  @Async
  @Override
  public void removeApiPermissions(Long roleId) {
    UserDto userDto = new UserDto();
    userDto.setRoleId(roleId);
    userDto.setStatus(UserStatusEnum.ENABLED.getFlag());
    RestResponse<List<User>> restResponse = userRemoteService.listUsersByRoleId(userDto);
    if(restResponse.flag){
      List<User> users = restResponse.getData();
      users.forEach(user -> {
        cacheManger.setLogout(user.getAccount());
       // cacheManger.removeLoginCache(user.getAccount());
      });
    }
  }

  /**
   * 根据用户Id查询角色列表
   * @param roleVo
   * @return
   */
  @Override
  public RestResponse<List<Role>> listRolesByUserId(RoleVo roleVo) {
    return roleRemoteService.listRolesByUserId(convert(roleVo));
  }


  /**
   * 转换成 RoleDto
   * @param roleVo
   * @return
   */
  private RoleDto convert(RoleVo roleVo){
    RoleDto roleDto = new RoleDto();
    CommonUtils.copyProperties(roleVo, roleDto);
    roleDto.setOperatorId(roleVo.getSysUserId());
    roleDto.setOperatorName(roleVo.getSysRealName());
    return roleDto;
  }
}
